<?php 
include_once("../dao/dbHelper.php");
abstract class Service
{
  protected $sql;
  protected $db;
  abstract function create($arrParams);
  protected function executeUpdate($arrParams)
  {
    try
    {
     self::init();
     return $this->db->insertRecord($this->sql,$arrParams);
    }
    catch(PDOException $pe)
    {
      throw $pe;
    }
    catch(Exception $ex)
    {
      //handler the clear
      self::clear();
      throw  $ex;
    }
  }
  /**
   *construct function ,initialization
   */
  function __construct()
  {
    try
    {
      if(!isset($this->db))$this->db = new dbHelper();
    }
    catch(Exception $ex)
    {
      throw $ex;
    }
  }
  /**
   *check the variable
   */
  private function init()
  {
   if(!isset($this->sql))throw new Exception("not set the sql yet");
   if(!isset($this->db))throw new Exception("could not init the dbHelper");
  }

  /**
   *<description>execute query
   *Fetch Arg FETCH::FETCH_BOTH
  */
  protected function query($arrValue=null,$ftype=PDO::FETCH_BOTH)
  {
    try
    {
      $this->init();
      return $this->db->getRecords($this->sql,$arrValue,$ftype);      
    }
    catch(PDOException $pe)
    {
      throw $pe;
    }
    catch(Exception $ex)
    {
      throw $ex;
    }
  }
  
  /**
   *clear the variable
   */
  protected function clear()
  {
    if(isset($this->db))unset($this->db);
  }
  //send msg to page
  public function msgFunc($arrMsg)
  {
   foreach($arrMsg as $key => $value)
   {
      $arrMsg[$key]=urlencode($value);
    }
   return json_encode($arrMsg);
  }
}
 ?>